<template>
  <div class=''>
    <!-- 按钮 -->
    <div class="btns">
      <div style="text-align:center">
        <el-form size="small" inline :model="param"> 
          <el-form-item label="名称">
            <el-input v-model="param.name"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" @click="loadDataset">搜索</el-button>
            <el-button size="small" type="primary" @click="toAddHandler" icon="el-icon-plus">新增</el-button>
            <el-button size="small" type="primary" @click="downloadHandler" icon="el-icon-download">导出</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!-- 按钮 -->
    <el-table size="small" :data="datasets">
      <el-table-column label="序号" type="index" :index="1" width="60" align="center"></el-table-column>
      <el-table-column label="封面" prop="photo" align="center" width="140px">
        <template slot-scope="scope">
          <div style="width:180px;border-radius:5px;height:80px;over-flow:hidden" >
            <img :src="scope.row.photo" alt="" style="width:100%">
          </div>
        </template>
      </el-table-column>
      <el-table-column label="名称" prop="name" align="center"></el-table-column>
      <el-table-column label="简介" prop="introduce" align="center"></el-table-column>
      <el-table-column label="照片" prop="photo" align="center" width="320px" height="200px">
           <template slot-scope="scope">
          <div style="width:300px;border-radius:5px;height:150px;over-flow:hidden;">
            <img :src="scope.row.photo" :preview-src-list="srcList" alt="" style="width:100%;">
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150px" align="center">
        <template slot-scope="scope">
          <el-button-group>
            <el-button type="warning" size="medium" icon="el-icon-edit" circle @click="editHandler(scope.row)"></el-button>
            <el-button type="success" size="medium" icon="el-icon-s-promotion" circle @click="toEnterHandler(scope.row)"></el-button>
            <el-button type="danger"  size="medium" icon="el-icon-delete" circle @click="toDeleteHandler(scope.row)"></el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { del,get } from '@/utils/request'
export default {
  // 组件名称
  name: 'demo',
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data () {
   return {
     param:{},
     datasets:[]
   }
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    downloadHandler(){
      window.location.href="http://39.101.198.99:8888/dataset/download?name="+this.param.name
    },
    editHandler(row){
        // 跳转到修改
        // 携带当前行
        this.$router.push({
          path:"/file/create",
          query:row
        })
    },
    toEnterHandler(row){
      this.$router.push({
        path:"/file/dataset_file",     // 页面跳转的 - 与后端路由无关
        query:{datasetId:row.id}
      })
    },
    toDeleteHandler(row){
         this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let url = "http://39.101.198.99:8888/dataset/deleteById";
        del(url, { id: row.id }).then((resp) => {
          this.$message({ type: "success", message: resp.message });
          this.loadDataset();
        });
      });
    },
    toAddHandler(){
      this.$router.push("/file/create")
    },
    loadDataset(){
      let url = "http://39.101.198.99:8888/dataset/query"   // 后端接口
      get(url,this.param).then(resp => {
        this.datasets = resp.data;
      })
    }
  },
  // 以下是生命周期钩子
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    this.loadDataset();
  },
  /**
  * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
  */
  mounted () {
  },
  /**
  * 实例销毁之前调用。在这一步，实例仍然完全可用。
  */
  beforeDestroy () {
  },
  /**
  * Vue 实例销毁后调用。调用后，Vue 实例指示的所有东西都会解绑定，
  * 所有的事件监听器会被移除，所有的子实例也会被销毁。
  */
  destroyed () {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style scoped>

</style>
